package com.iplant.ams.service.po.dms.scada.status;

import com.fasterxml.jackson.annotation.JsonProperty;
import com.iplant.ams.service.basicenum.dms.scada.DMSDeviceStatusEnum;
import com.iplant.base.utils.general.CalendarTool;

import java.util.Calendar;
import java.util.HashMap;
import java.util.Map;

public class DMSDeviceStatisticsInfo {
    /// <summary>
    /// 设备编码
    /// </summary>
    public String DeviceNo = "";


    public int DeviceID = 0;

    /// <summary>
    ///  设备名称
    /// </summary>
    public String DeviceName = "";

    /// <summary>
    /// 固定资产编码
    /// </summary>
    public String AssetNo = "";

    /// <summary>
    /// 设备型号ID
    /// </summary>
    public int ModelID = 0;
    /// <summary>
    /// 设备型号名称
    /// </summary>
    public String ModelName = "";
    /// <summary>
    /// 设备型号ID
    /// </summary>
    public String ModelNo = "";


    public String Remark = "";

    /// <summary>
    /// 开始时刻
    /// </summary>
    public Calendar StatStartDate = CalendarTool.getCalendar(2000, 0, 1);

    /// <summary>
    /// 结束时刻
    /// </summary>
    public Calendar StatEndDate = CalendarTool.getCalendar(2000, 0, 1);

    /// <summary>
    /// 统计周期日期
    /// </summary>

    public Calendar StatDate = CalendarTool.getCalendar(2000, 0, 1);


    public Map<String, Integer> StatusTimesDic = new HashMap<String, Integer>();

    public Map<String, Integer> StatusDurationDic = new HashMap<String, Integer>();

    /// <summary>
    /// 计划工作时长   工作日历的工作日*8小时
    /// </summary>
    public int PlanDuration = 0;

    /// <summary>
    /// 故障次数
    /// </summary>
    public int FailTimes = 0;


    /// <summary>
    /// 开机时长
    /// </summary>
    public int TurnOnDuration = 0;


    /// <summary>
    /// 运行时长
    /// </summary>
    public int RunDuration = 0;


    /// <summary>
    /// 故障时长
    /// </summary>
    public int AlarmDuration = 0;


    /// <summary>
    /// 开机率
    /// </summary>

    public double TurnOnRate = 0.0;


    /// <summary>
    /// 故障率
    /// </summary>
    public double AlarmRate = 0.0;

    /// <summary>
    /// 有效率
    /// </summary>
    public double Efficiency = 0.0;


    /// <summary>
    /// 平均无故障时间  = 无故障时间/故障次数 =（开机时间-故障时间）/故障次数
    /// </summary>
    public int MTTF = 0;

    /// <summary>
    /// 平均故障间隔时间  = 开机时间/故障次数
    /// </summary>
    public int MTBF = 0;
    /// <summary>
    /// 平均修复时间   =  故障时间/故障次数
    /// </summary>
    public int MTTR = 0;

    /// <summary>
    /// 流水线人数
    /// </summary>
    public int OperatorTotal = 0;

    /// <summary>
    /// 合格率
    /// </summary>
    public double GoodRate = 0.0;

    /// <summary>
    /// 上料数
    /// </summary>
    public double FQTYFeeding = 0;

    /// <summary>
    /// 合格数
    /// </summary>
    public double FQTYGood = 0.0;
    /// <summary>
    /// 不合格数
    /// </summary>
    public double FQTYBad = 0.0;

    /// <summary>
    /// 总数
    /// </summary>
    public double FQTYTotal = 0.0;


    public double OEE = 0.0;

    /// <summary>
    /// 理论节拍
    /// </summary>
    public double PartInterval = 35 * 60;

    /// <summary>
    /// 节拍性能效率
    /// </summary>
    public double PartRate = 0.0;


    @JsonProperty("DeviceNo")
    public String getDeviceNo() {
        return DeviceNo;
    }

    public void setDeviceNo(String deviceNo) {
        DeviceNo = deviceNo;
    }

    @JsonProperty("DeviceID")
    public int getDeviceID() {
        return DeviceID;
    }

    public void setDeviceID(int deviceID) {
        DeviceID = deviceID;
    }

    @JsonProperty("DeviceName")
    public String getDeviceName() {
        return DeviceName;
    }

    public void setDeviceName(String deviceName) {
        DeviceName = deviceName;
    }

    @JsonProperty("AssetNo")
    public String getAssetNo() {
        return AssetNo;
    }

    public void setAssetNo(String assetNo) {
        AssetNo = assetNo;
    }

    @JsonProperty("ModelID")
    public int getModelID() {
        return ModelID;
    }

    public void setModelID(int modelID) {
        ModelID = modelID;
    }

    @JsonProperty("ModelName")
    public String getModelName() {
        return ModelName;
    }

    public void setModelName(String modelName) {
        ModelName = modelName;
    }

    @JsonProperty("ModelNo")
    public String getModelNo() {
        return ModelNo;
    }

    public void setModelNo(String modelNo) {
        ModelNo = modelNo;
    }

    @JsonProperty("Remark")
    public String getRemark() {
        return Remark;
    }

    public void setRemark(String remark) {
        Remark = remark;
    }

    @JsonProperty("StatStartDate")
    public Calendar getStatStartDate() {
        return StatStartDate;
    }

    public void setStatStartDate(Calendar statStartDate) {
        StatStartDate = statStartDate;
    }

    @JsonProperty("StatEndDate")
    public Calendar getStatEndDate() {
        return StatEndDate;
    }

    public void setStatEndDate(Calendar statEndDate) {
        StatEndDate = statEndDate;
    }

    @JsonProperty("StatDate")
    public Calendar getStatDate() {
        return CalendarTool.getDate(StatStartDate);
    }

    public void setStatDate(Calendar statDate) {
        StatDate = statDate;
    }

    @JsonProperty("StatusTimesDic")
    public Map<String, Integer> getStatusTimesDic() {
        return StatusTimesDic;
    }

    public void setStatusTimesDic(Map<String, Integer> statusTimesDic) {
        StatusTimesDic = statusTimesDic;
    }

    @JsonProperty("StatusDurationDic")
    public Map<String, Integer> getStatusDurationDic() {
        return StatusDurationDic;
    }

    public void setStatusDurationDic(Map<String, Integer> statusDurationDic) {
        StatusDurationDic = statusDurationDic;
    }

    @JsonProperty("PlanDuration")
    public int getPlanDuration() {
        return PlanDuration;
    }

    public void setPlanDuration(int planDuration) {
        PlanDuration = planDuration;
    }

    @JsonProperty("FailTimes")
    public int getFailTimes() {
        if (StatusTimesDic != null && StatusTimesDic.containsKey((DMSDeviceStatusEnum.Alarm.getValue()) + "")) {
            return StatusTimesDic.get((DMSDeviceStatusEnum.Alarm.getValue()) + "");
        }
        return 0;
    }

    public void setFailTimes(int failTimes) {
        FailTimes = failTimes;
    }

    @JsonProperty("TurnOnDuration")
    public int getTurnOnDuration() {
        if (StatusDurationDic != null && StatusDurationDic.containsKey((DMSDeviceStatusEnum.TurnOn.getValue()) + "")) {
            return StatusDurationDic.get((DMSDeviceStatusEnum.TurnOn.getValue()) + "");
        }
        return 0;
    }

    public void setTurnOnDuration(int turnOnDuration) {
        TurnOnDuration = turnOnDuration;
    }

    @JsonProperty("RunDuration")
    public int getRunDuration() {
        if (StatusDurationDic != null && StatusDurationDic.containsKey((DMSDeviceStatusEnum.Run.getValue()) + "")) {
            return StatusDurationDic.get(((int) DMSDeviceStatusEnum.Run.getValue()) + "");
        }
        return 0;
    }

    public void setRunDuration(int runDuration) {
        RunDuration = runDuration;
    }

    @JsonProperty("AlarmDuration")
    public int getAlarmDuration() {
        if (StatusDurationDic != null && StatusDurationDic.containsKey((DMSDeviceStatusEnum.Alarm.getValue()) + "")) {
            return StatusDurationDic.get((DMSDeviceStatusEnum.Alarm.getValue()) + "");
        }
        return 0;
    }

    public void setAlarmDuration(int alarmDuration) {
        AlarmDuration = alarmDuration;
    }

    @JsonProperty("TurnOnRate")
    public double getTurnOnRate() {
        if (PlanDuration <= 0 || TurnOnDuration <= 0)
            return 0;
        if (TurnOnDuration >= PlanDuration)
            return 1.0;

        return TurnOnDuration * 1.0 / PlanDuration;
    }

    public void setTurnOnRate(double turnOnRate) {
        TurnOnRate = turnOnRate;
    }

    @JsonProperty("AlarmRate")
    public double getAlarmRate() {
        if (AlarmDuration <= 0 || TurnOnDuration <= 0)
            return 0;
        if (AlarmDuration >= TurnOnDuration)
            return 1.0;

        return AlarmDuration * 1.0 / TurnOnDuration;
    }

    public void setAlarmRate(double alarmRate) {
        AlarmRate = alarmRate;
    }

    @JsonProperty("Efficiency")
    public double getEfficiency() {
        if (RunDuration <= 0 || TurnOnDuration <= 0)
            return 0;
        if (RunDuration >= TurnOnDuration)
            return 1.0;
        return RunDuration * 1.0 / TurnOnDuration;
    }

    public void setEfficiency(double efficiency) {
        Efficiency = efficiency;
    }

    @JsonProperty("MTTF")
    public int getMTTF() {
        if (TurnOnDuration <= 0 || AlarmDuration > TurnOnDuration)
            return 0;

        return (TurnOnDuration - AlarmDuration) / (FailTimes <= 0 ? 1 : FailTimes);
    }

    public void setMTTF(int MTTF) {
        this.MTTF = MTTF;
    }

    @JsonProperty("MTBF")
    public int getMTBF() {
        if (TurnOnDuration <= 0)
            return 0;

        return TurnOnDuration / (FailTimes <= 0 ? 1 : FailTimes);
    }

    public void setMTBF(int MTBF) {
        this.MTBF = MTBF;
    }

    @JsonProperty("MTTR")
    public int getMTTR() {
        if (AlarmDuration <= 0)
            return 0;
        return AlarmDuration / (FailTimes <= 0 ? 1 : FailTimes);
    }

    public void setMTTR(int MTTR) {
        this.MTTR = MTTR;
    }

    @JsonProperty("OperatorTotal")
    public int getOperatorTotal() {
        return OperatorTotal;
    }

    public void setOperatorTotal(int operatorTotal) {
        OperatorTotal = operatorTotal;
    }

    @JsonProperty("GoodRate")
    public double getGoodRate() {
        if (FQTYGood <= 0 || FQTYTotal <= 0)
            return 0.0;
        if (FQTYGood > FQTYTotal)
            return 1.0;
        return FQTYGood / FQTYTotal;
    }

    public void setGoodRate(double goodRate) {
        GoodRate = goodRate;
    }

    @JsonProperty("FQTYFeeding")
    public double getFQTYFeeding() {
        return FQTYFeeding;
    }

    public void setFQTYFeeding(double FQTYFeeding) {
        this.FQTYFeeding = FQTYFeeding;
    }

    @JsonProperty("FQTYGood")
    public double getFQTYGood() {
        if (FQTYTotal <= 0 || FQTYBad > FQTYTotal)
            return 0;
        return FQTYTotal - FQTYBad;
    }

    public void setFQTYGood(double FQTYGood) {
        this.FQTYGood = FQTYGood;
    }

    @JsonProperty("FQTYBad")
    public double getFQTYBad() {
        return FQTYBad;
    }

    public void setFQTYBad(double FQTYBad) {
        this.FQTYBad = FQTYBad;
    }

    @JsonProperty("FQTYTotal")
    public double getFQTYTotal() {
        return FQTYTotal;
    }

    public void setFQTYTotal(double FQTYTotal) {
        this.FQTYTotal = FQTYTotal;
    }

    @JsonProperty("OEE")
    public double getOEE() {
        return GoodRate * Efficiency * PartRate;
    }

    public void setOEE(double OEE) {
        this.OEE = OEE;
    }

    @JsonProperty("PartInterval")
    public double getPartInterval() {
        return PartInterval;
    }

    public void setPartInterval(double partInterval) {
        PartInterval = partInterval;
    }

    @JsonProperty("PartRate")
    public double getPartRate() {
        if (PartInterval <= 0 || FQTYTotal <= 0 || RunDuration <= 0)
            return 0.0;
        if ((PartInterval * FQTYTotal) > RunDuration)
            return 1.0;


        return (PartInterval * FQTYTotal) / RunDuration;
    }

    public void setPartRate(double partRate) {
        PartRate = partRate;
    }
}
